package com.example.academicsystem.server.service.impl;

import com.example.academicsystem.constant.MessageConstant;
import com.example.academicsystem.exception.LoginFailedException;
import com.example.academicsystem.exception.UpdateException;
import com.example.academicsystem.pojo.dto.*;
import com.example.academicsystem.pojo.vo.AdminLoginVo;
import com.example.academicsystem.pojo.vo.StudentLoginVo;
import com.example.academicsystem.pojo.vo.TeacherLoginVo;
import com.example.academicsystem.server.dao.UserMapper;
import com.example.academicsystem.server.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;
   /* @Autowired
    private BcryptEncoder bcryptEncoder;*/
    /**
     * 学生端登录
     * @param studentLoginDto
     * @return
     * */
    public StudentLoginVo studentLogin(StudentLoginDto studentLoginDto) {
        if(studentLoginDto ==null || studentLoginDto.getStudentId()==null|| studentLoginDto.getPassword()==null){
            throw new LoginFailedException(MessageConstant.PASSWORD_ERROR);
        }
        StudentLoginVo studentLoginVo= userMapper.studentLogin(studentLoginDto);
        if(studentLoginVo==null){throw new LoginFailedException(MessageConstant.LOGIN_FAILED);}
        return studentLoginVo;
    }

    /**
     * 教师端登录
     * @param teacherLoginDto
     * @return
    * */
    public TeacherLoginVo teacherLogin(TeacherLoginDto teacherLoginDto) {
        if(teacherLoginDto==null||teacherLoginDto.getTeacherId()==null||teacherLoginDto.getPassword()==null){
            throw new LoginFailedException(MessageConstant.LOGIN_FAILED);
        }
        TeacherLoginVo teacherLoginVo=userMapper.teacherLogin(teacherLoginDto);
        if (teacherLoginVo==null){throw new LoginFailedException(MessageConstant.LOGIN_FAILED);}
        return teacherLoginVo;
    }

    /**
     * 管理员登录
     * @param adminLoginDto
     * @return
    * */
    public AdminLoginVo adminLogin(AdminLoginDto adminLoginDto) {
      AdminLoginVo adminLoginVo =userMapper.adminLogin(adminLoginDto);
      if (adminLoginVo==null){throw new LoginFailedException(MessageConstant.LOGIN_FAILED);}
      return adminLoginVo;

    }

    /**
     * 密码修改
     * */
    public void pwdUpdate(PwdUpdateDto pwdUpdateDto) {
        if (pwdUpdateDto.getId()==null ||pwdUpdateDto.getOldPwd()==null||pwdUpdateDto.getIdentify()==null)
            throw new UpdateException(MessageConstant.PARAM_NULL);
        Integer identify = pwdUpdateDto.getIdentify();
        // admin:0   student:1  teacher:2
        if (identify==0)
            userMapper.adminPwdUpdate(pwdUpdateDto);
        else if (identify==1)
            userMapper.studentPwdUpdate(pwdUpdateDto);
        else if (identify==2)
            userMapper.teacherPwdUpdate(pwdUpdateDto);
        else
            throw new UpdateException("identify is false");
    }

    /**
     * register admin：0  student：1  teacher：2
     * */
/*    public void userRegister(RegisterDto registerDto) {
       registerDto.setPassword(bcryptEncoder.encode(registerDto.getPassword()));
       if (registerDto.getIdentify()==null||registerDto.getUsername()==null)
           throw new InsertException(MessageConstant.PARAM_NULL);
       switch (registerDto.getIdentify()){
           case 0:
               userMapper.adminRegister(registerDto);
               break;
           case 1:
               userMapper.studentRegister(registerDto);
           case 2:
               userMapper.teacherRegister(registerDto);
           default:
               throw new InsertException("identify is false");
       }
    }*/
}
